package com.sduept.bigdata.test;

import java.io.File;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;

/**
 * 适用于web项目的war类型测试
 *
 */
public class WebArchiveDeployment {


    private WebArchive webArchive;
    
    
    public WebArchiveDeployment() {
        webArchive = ShrinkWrap.create(WebArchive.class, "test.war");
    }
    
    public WebArchiveDeployment withALL() {
    	withDefaultPackages().withPersistence().withResource().withPomDependencies().withBeanXml();
    	return this;
    }

    
    public WebArchiveDeployment withDefaultPackages() {
    	//需要测试的类所在的包，包含类引用的所有包
    	webArchive = webArchive.addPackages(true,"com/sduept");
    	return this;
    }
    public WebArchiveDeployment withPersistence() {
        webArchive = webArchive.addAsResource("META-INF/test-persistence.xml", "META-INF/persistence.xml").addAsWebInfResource(
            "test-ds.xml", "test-ds.xml");
        return this;
    }
    public WebArchiveDeployment withResource() {
    	webArchive = webArchive.addAsResource("META-INF/jboss-deployment-structure.xml", "META-INF/jboss-deployment-structure.xml");
    	return this;
    }
    
    public WebArchiveDeployment withBeanXml() {
    	//需要测试的类所在的包，包含类引用的所有包
    	webArchive = webArchive.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
    	return this;
    }
    
    public WebArchiveDeployment withPomDependencies() {
    	File[] files = Maven.resolver().loadPomFromFile("test-pom.xml")
		        .importCompileAndRuntimeDependencies().resolve().withTransitivity().asFile();
        webArchive = webArchive.addAsLibraries(files);
        return this;
    } 

  

    public WebArchive getArchive() {
        return webArchive;
    }
}
